package chat;

import java.util.ArrayList;

/**
 * Synchronised resource for use in ACE4.
 * @author Deryk Egan / 200907381
 */

public class SyncChat {
	
	ArrayList<Message> messages;
	
	/**
	 * Construct new synchronised chat instance.
	 */
	public SyncChat(){
		messages = new ArrayList<Message>();
	}
	
	/**
	 * Add new message.
	 * @param message Message to add as String
	 */
	public synchronized void addMessage(Message message){
		messages.add(message);
	}
	
	/**
	 * The number of messages stored.
	 * @return
	 */
	public synchronized int getCount(){
		return messages.size();
	}
	
	/**
	 * Gets all messages added since the count number
	 * specified.
	 * @param count
	 * @return ArrayList of messages if any available, null if no messages.
	 */
	public synchronized ArrayList<Message> getMessagesSince(int count){
		if(count > messages.size())
			return null;
		
		ArrayList<Message> messagesToReturn = new ArrayList<Message>();
		for(int i = count; i < messages.size(); i++){
			messagesToReturn.add(messages.get(i));
		}
		return messagesToReturn;
	}


}
